package com.example.higo.api.service;

import com.example.higo.api.ApiService;
import com.example.higo.api.service.homepagecallback.HomePageCallBack;
import com.example.higo.api.service.maishoucallback.MaishouCallBack;
import com.example.higo.api.service.shishangquancallback.ShishangquanCallBack;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * Created by wj on 2016/11/6.
 */

public class RetrofitManager {
    private static volatile RetrofitManager instance = null;
    public Retrofit retrofit;
    private Gson gson;
    OkHttpClient client;
    private static final int DEFAULT_READ_TIMEOUT = 15 * 1000;
    private static final int DEFAULT_CONNECT_TIMEOUT = 15 * 1000;
    private static final int DEFAULT_WRITE_TIMEOUT = 15 * 1000;


    private RetrofitManager() {
        initOkhttpClient();
        initRetrofit();
    }

    public static RetrofitManager getInstance() {
        if (instance == null) {
            synchronized (RetrofitManager.class) {
                if (instance == null) {
                    instance = new RetrofitManager();
                }
            }
        }
        return instance;
    }

    public void initRetrofit() {
        if (retrofit == null) {

            /**
             *  获得 Retrofit 对象
             */
            retrofit = new Retrofit.Builder()
                    .client(client) //  自定义底层OkHttpClient
                    .baseUrl(ApiService.BASE_URL) //设置BaseUrl
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) //设置响应式适配工厂
                    .addConverterFactory(GsonConverterFactory.create(getGson())) // 设置解析转换器
                    .build();  //创建　Retrofit 对象
        }
    }

    /**
     * 设置连接超时
     */
    private void initOkhttpClient() {
        if (client == null) {
            client = new OkHttpClient.Builder()
                    .readTimeout(DEFAULT_READ_TIMEOUT, TimeUnit.MILLISECONDS)
                    .connectTimeout(DEFAULT_CONNECT_TIMEOUT, TimeUnit.MILLISECONDS)
                    .writeTimeout(DEFAULT_WRITE_TIMEOUT, TimeUnit.MILLISECONDS)
                    .build();
        }
    }

    /**
     * 定制Gson
     *
     * @return
     */
    public Gson getGson() {
        if (gson == null) {
            gson = new GsonBuilder()
                    .serializeNulls()
                    .setDateFormat("yyyy-MM-dd") //设置日期格式
                    .create();
        }
        return gson;
    }

    /**
     * 创建HomePageCallBack 的对象
     */

    public HomePageCallBack getHomePageCallBack(){
        return retrofit.create(HomePageCallBack.class);
    }

    /**
     * 创建ShishangquanCallBack 对象
     */
    public ShishangquanCallBack getShishangquanCallBack(){
        return retrofit.create(ShishangquanCallBack.class);
    }

    /**
     * 创建MaishouCallBack 对象
     */
    public MaishouCallBack getMaishouCallBack(){
        return retrofit.create(MaishouCallBack.class);
    }


}
